<%- include('css') %>
    <div id="app" style="width:980px">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>权限编辑</legend>
        </fieldset>

        <form class="layui-form" action="" lay-filter="form">

            <div class="layui-form-item">
                <label class="layui-form-label">权限名</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required|name" placeholder="请输入权限名(长度1-20)" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限编号</label>
                <div class="layui-input-block">
                    <input type="text" name="code" lay-verify="required|code" autocomplete="off" placeholder="请输入权限编号(仅支持字母不支持中文和字符)" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限路径</label>
                <div class="layui-input-inline">
                    <input type="text" name="path" lay-verify="required|path" autocomplete="off" placeholder="请输入权限路径" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">例:/admin/接口 或 /admin/接口/方法名</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input type="text" name="remark" lay-verify="" autocomplete="off" placeholder="请输入备注" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" v-bind:disabled="isButtonDisabled" lay-filter="update">保存</button>
                    <button type="button" v-on:click="doclose" class="layui-btn layui-btn-primary">关闭</button>
                </div>
            </div>
        </form>
    </div>
    <%- include('script') %>
        <script>
            var formSelects = layui.formSelects;
            var vm = new Vue({
                el: "#app",
                data: function () {
                    return {
                        isButtonDisabled: false,
                        id:'<%=id%>',

                    }

                },
                mounted: function () {
                    let self = this

                    layui.use(['form'], function () {
                        var form = layui.form;

                        //定义验证规则
                        form.verify({
                            path: function(value, item){ //value：表单的值、item：表单的DOM对象
                                if(!new RegExp("^/[a-z]+/([a-z]+|[a-z]+/[a-z]+|[a-z]+/[a-z]+/[a-z]+)$").test(value)){
                                    return '路径必须以\'/\'开头,且只能包含\'/和小写字母\'';
                                }
                            },
                            //我们既支持上述函数式的方式，也支持下述数组的形式
                            //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
                            code: [
                                /^[A-Za-z]+$/
                                ,'编号只能包含字母,不能有中文或者下划线等字符'
                            ],
                            name:[/^.{1,20}$/,'只能填写长度为1-20的所有字符']
                        });

                        var load = layer.load();
                        $.ajax({
                        url:'/admin/auth/'+self.id,
                        type:'GET',
                        data:{},
                        success:(res)=>{
                            if(res.code === 200){

                                form.val("form",res.data);
                            }else{
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                parent.layer.msg('获取数据失败：'+res.error+' -- '+res.code,{icon:2});
                            }
                        },
                        complete:()=>{
                            layer.close(load);
                        }
                        });

                        //监听提交
                        form.on('submit(update)', function (data) {
                            self.isButtonDisabled = true;
                            $.ajax({
                            url:'/admin/auth/'+self.id,
                            type:'PUT',
                            data:{data:data.field},
                            success:(res)=>{
                                if(res.code === 200){
                                    parent.vm.iframeupdate()
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                    parent.layer.msg('修改成功!',{icon:1})
                                }else{
                                    layer.msg('修改失败：'+res.error+' -- '+res.code,{icon:2});
                                }
                                self.isButtonDisabled = false;
                            }
                            });
                            return false;
                        })
                    });
                },
                methods: {
                    doclose: function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    },
                }
            })
        </script>
